package cn.codesugar.service

import android.app.job.JobInfo
import android.app.job.JobScheduler
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.*
import androidx.appcompat.app.AppCompatActivity
import android.util.Log
import cn.codesugar.service.MyJobService.Companion.MY_JOB_SERVICE_JOB_ID
import cn.codesugar.service.databinding.ActivityMainBinding

private const val TAG = "MainActivity"

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private val updateText = 1
//    private val handler = object : Handler(Looper.getMainLooper()) {
//        override fun handleMessage(msg: Message) {
//            // 在这里可以进行UI操作
//            when (msg.what) {
//                updateText -> binding.textView.text = "Nice to meet you"
//            }
//        }
//    }
    private lateinit var downloadBinder: MyService.DownloadBinder
    private var connection = object: ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            downloadBinder = service as MyService.DownloadBinder
            downloadBinder.startDownload()
            val progress = downloadBinder.getProgress()
            Log.e(TAG, "ComponentName:$name onServiceConnected executed, progress:$progress")
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            Log.e(TAG, "ComponentName:$name onServiceDisconnected executed")
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
/*        // 继承Thread
        MyThread().start()
        // 实现Runnable接口
        val myRunnable = MyRunnable()
        Thread(myRunnable).start()
        // Lambda方式
        Thread {
            Log.e("Thread", "Time:${System.currentTimeMillis()}")
        }.start()
        // kotlin
        thread {
            Log.e("thread", "Time:${System.currentTimeMillis()}")
        }
        binding.changeTextBtn.setOnClickListener {
            thread {
                val msg = Message()
                msg.what = updateText
                handler.sendMessage(msg)
            }
        }*/
        val intent = Intent(this, MyService::class.java)
        binding.startServiceBtn.setOnClickListener {
            startService(intent) // 启动Service
        }
        binding.stopServiceBtn.setOnClickListener {
            stopService(intent) // 停止Service
        }
        binding.bindServiceBtn.setOnClickListener {
            bindService(intent, connection, Context.BIND_AUTO_CREATE) // 绑定Service
        }
        binding.unbindServiceBtn.setOnClickListener {
            unbindService(connection) // 解绑Service
        }
        binding.startIntentServiceBtn.setOnClickListener {
            // 打印主线程的id
            Log.e(TAG, "Thread id is ${Thread.currentThread().name}")
            val intentService = Intent(this, MyIntentService::class.java)
            startService(intentService)
        }
        binding.startJobIntentServiceBtn.setOnClickListener {
            // 打印主线程的id
            Log.e(TAG, "Thread id is ${Thread.currentThread().name}")
            val jobIntentService = Intent(this, MyJobIntentService::class.java)
            startService(jobIntentService)
        }
        binding.startJobServiceBtn.setOnClickListener {
            val myJobServiceComponentName = ComponentName(this, MyJobService::class.java)
            val jobBuilder = JobInfo.Builder(MY_JOB_SERVICE_JOB_ID, myJobServiceComponentName)
            Log.e(TAG, "SDK_INT:${Build.VERSION.SDK_INT}")
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                val scheduler = this.getSystemService(JobScheduler::class.java)
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
                    jobBuilder.setMinimumLatency(10*1000)
                }else{
                    jobBuilder.setPeriodic(10*1000)
                }
                val myJob = jobBuilder.build()
                scheduler.schedule(myJob)
            } else {
                Log.e(TAG, "API < 23")
            }
        }
    }
}